package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mapper.ClazzMapper;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.Emp;
import com.itheima.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ClazzServiceImpl implements ClazzService {

    @Autowired
    private ClazzMapper clazzMapper;


    @Override
    public IPage<Clazz> pageQuery(String name, LocalDate begin, LocalDate end, int page, int pageSize) {
        // 构建分页对象
        IPage<Clazz> pageInfo = new Page<>(page, pageSize);
        // 构建查询条件
        LambdaQueryWrapper<Clazz> queryWrapper = new LambdaQueryWrapper<>();
        // 班级名称模糊查询
        if (name != null && !name.isEmpty()) {
            queryWrapper.like(Clazz::getName, name);
        }
        // 结课时间范围查询
        if (begin != null) {
            queryWrapper.ge(Clazz::getEndDate, begin);
        }
        if (end != null) {
            queryWrapper.le(Clazz::getEndDate, end);
        }
        // 执行分页查询
        return clazzMapper.selectPage(pageInfo, queryWrapper);
    }

    @Override
    public void save(Clazz clazz) {
        // 补全创建时间和更新时间
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());
        // 插入数据
        clazzMapper.insert(clazz);
    }

    @Override
    public Clazz getById(Long id) {
        return clazzMapper.selectById(id);
    }

    @Override
    public void update(Clazz clazz) {
        // 补全更新时间
        clazz.setUpdateTime(LocalDateTime.now());
        // 更新数据
        clazzMapper.updateById(clazz);
    }

    @Override
    public void deleteById(Long id) {
        clazzMapper.deleteById(id);
    }

    @Override
    public List<Clazz> findAll() {
        return clazzMapper.selectList(null);
    }
}